﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using SharpBolt.Graphics;
using InductBase;

namespace SharpBolt.Serialization
{
    public class BaseTypesJsonConverter : JsonConverter
    {
        
        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue(value.ToString());
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;

            if (objectType == typeof(Color))
            {
                return Color.Parse(reader.Value.ToString());
            }

            if (objectType == typeof(Rec))
            {
                return Rec.Parse(reader.Value.ToString());
            }

            if (objectType == typeof(Vec2))
            {
                return Vec2.Parse(reader.Value.ToString());
            }

            if (objectType == typeof(Vec2i))
            {
                return Vec2i.Parse(reader.Value.ToString());
            }

            if (objectType == typeof(Transformation))
            {
                return Transformation.Parse(reader.Value.ToString());
            }

            throw new ApplicationException(String.Format("The given objectType {0} is not supported!", objectType));
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(Color) ||
                    objectType == typeof(Transformation) ||
                     objectType == typeof(Rec) ||
                     objectType == typeof(Vec2) ||
                     objectType == typeof(Vec2i);
        }
        
    }
}
